package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.dao.ErpCustomerCarMapper;
import com.monitor.dao.ErpMaterielCopyMapper;
import com.monitor.dao.ErpSaleRepairOrderMapper;
import com.monitor.dao.ErpSaleRepairRepairMapper;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.BlankUtil;
import com.monitor.util.DataUtil;
import com.monitor.util.IDUtils;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库物料 数量统计表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-09
 */
@RestController
@Transactional
@RequestMapping("/erpWarehouse")
@Api(description="仓库物料")
public class ErpWarehouseController {

    @Resource
    private  ErpMaterielService erpMaterielService;
    @Resource
    private ErpManagementService managementService;
    @Resource
    private ErpClientsService clientsService;
    @Resource
    private ErpClientsMaterielService materielService;
    @Resource
    private ErpMaterielFlowService flowService;
    @Resource
    private ErpCustomerWarehouseService customerWarehouseService;
    @Resource
    private ErpCustomerService customerService;
    @Resource
    private ErpClientsMaterielDetailsService detailsService;
    @Resource
    private ErpWarehouseService warehouseService;
    @Resource
    private ErpFlowClientsService flowClientsService;
    @Resource
    private ErpMaterielFlowSummaryService flowSummaryService;
    @Resource
    private ErpMaterielFlowMaterielService flowMaterielService;
    
    @Resource
    private ErpSaleRepairRepairMapper erpSaleRepairRepairMapper;

    @Resource
    private  ErpTireArchivesService erpTireArchivesService;

    @Resource
    private  ErpCustomerCarService erpCustomerCarService;

    @Resource
    private  ErpCarTireArchivesService erpCarTireArchivesService;

    @Resource
    private  ErpTireSensorService erpTireSensorService;
    
    @Resource
    private  ErpCustomerWarehouseService erpCustomerWarehouseService;

    @Resource
    private ErpCustomerCarMapper erpCustomerCarMapper;
    
    @Resource
    private ErpSaleRepairOrderMapper erpSaleRepairOrderMapper;
    
    @Resource
    private ErpMaterielCopyMapper erpMaterielCopyMapper;
    
    @RequestMapping("/selectWuLiaoBaoFei")
    @ApiOperation("经营管理-物料库存设置预警值")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "wareId", value = "仓库id", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "materielId", value = "数组[0]", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "materielWarnAmount", value = "预警值", required = false, dataType = "int", paramType = "query")
    })
    public Result<List<Map<String,Object>>> selectWuLiaoBaoFei(int materielId,int warehouseId){

        Result<List<Map<String,Object>>> result=new Result<>();
        try{
            List<ErpMateriel> erpMateriels = erpMaterielService.selectList(new EntityWrapper<ErpMateriel>().eq("warehouse_id", warehouseId).eq("id", materielId).eq("delect_type", 1));
            erpMateriels.parallelStream().forEach(materiel->{
//                materiel

            });
        }catch (Exception e){
            e.printStackTrace();
        }



        return result;
    }

    @RequestMapping("/backWarehouseByClientId")
    @ApiOperation("根据采购订单id出库!!出库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custId", value = "当前客户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "clientId", value = "采购订单ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> backWarehouseByClientId(Integer clientId,Integer custId) {
        Result<Integer> result=new Result<Integer>();
        try{

            ErpCustomer customer=customerService.selectById(custId);//出库客户信息
            ErpClients clients=clientsService.selectById(clientId);//物料采购单
            ErpCustomer goCustomer=customerService.selectById(clients.getCustomerId());//入库客户信息
            List<ErpClientsMateriel> materielList=materielService.selectList(new EntityWrapper<ErpClientsMateriel>().eq("clients_id",clientId));//查询需要出库的物料
            ErpCustomerWarehouse customerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",custId));//出库客户仓库
            ErpCustomerWarehouse goCustomerWarehouse=customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id",goCustomer.getId()));//入库客户仓库

            //物料流转存放流转数据
            ErpMaterielFlow flow=new ErpMaterielFlow();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-mm-dd");
            String format = simpleDateFormat.format(new Date());
            flow.setMaterielFlowDate(format);

            //查到订单最后一个并截取字符串然后五位然后在添加的时候+1
            List<ErpClients> erpMaterielFlows = clientsService.selectList(new EntityWrapper<ErpClients>());
            String substring = erpMaterielFlows.get(erpMaterielFlows.size() - 1).getSheetId().substring(erpMaterielFlows.get(erpMaterielFlows.size() - 1).getSheetId().length() - 5);
            flow.setMaterielFlowSheetId("CGDD"+IDUtils.genOrderId()+IDUtils.addOne(substring));


            flow.setSourceWarehouseId(customerWarehouse.getId());
            flow.setSourceWarehouseName(customerWarehouse.getWarName());
            flow.setSourceCustomerId(custId);
            flow.setSourceCustomerName(customer.getCusName());
            flow.setWarehouseId(goCustomerWarehouse.getId());
            flow.setWarehouseName(goCustomerWarehouse.getWarName());
            flow.setCustomerId(clients.getCustomerId());
            flow.setCustomerName(goCustomer.getCusName());
            flow.setFlowType("被动出库");
            flow.setFlowDirection(0);//0入库
            flow.setTheId(goCustomerWarehouse.getCustomerId());
            flowService.insert(flow);
            //循环修改出库得物料库存数据 修改入库得物料库存数据（如果在入库得库里找不到入库得物料信息就新添加一个）
            for (int i = 0; i <materielList.size() ; i++) {
                /*******物料出库**********/
                //先查询物料属性信息
                ErpMaterielFlowMateriel materielFlowMateriel=flowMaterielService.selectOne(new EntityWrapper<ErpMaterielFlowMateriel>().eq("materiel_name",materielList.get(i).getMaterielName()).eq("materiel_brand",materielList.get(i).getMaterielBrand()).eq("materiel_Specifications",materielList.get(i).getMaterielSpecifications()).eq("materiel_model",materielList.get(i).getMaterielModel()).eq("materiel_place",materielList.get(i).getMaterielPlace()).eq("materiel_state",materielList.get(i).getMaterielState()).eq("materiel_unit",materielList.get(i).getMaterielUnit()));
                Integer outWarehouseRetauned=0;
                if(null==materielFlowMateriel){
                    materielFlowMateriel.setMaterielName(materielList.get(i).getMaterielName());
                    materielFlowMateriel.setMaterielBrand(materielList.get(i).getMaterielBrand());
                    materielFlowMateriel.setMaterielSpecifications(materielList.get(i).getMaterielSpecifications());
                    materielFlowMateriel.setMaterielModel(materielList.get(i).getMaterielModel());
                    materielFlowMateriel.setMaterielPlace(materielList.get(i).getMaterielPlace());
                    materielFlowMateriel.setMaterielState(materielList.get(i).getMaterielState());
                    materielFlowMateriel.setMaterielUnit(materielList.get(i).getMaterielUnit());
                    flowMaterielService.insert(materielFlowMateriel);
                }

                //根据物料属性信息id查询仓库物料信息
                ErpWarehouse warehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",customerWarehouse.getId()).eq("customer_id",customer.getId()).eq("materiel_id",materielFlowMateriel.getId()));
                Integer num=warehouse.getMaterielNumber();
                warehouse.setMaterielNumber(num-materielList.get(i).getMaterielAmount());
                warehouseService.updateById(warehouse);//修改出库的数据

                /*****物料入库**********/
                ErpWarehouse goWarehouse=warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",goCustomerWarehouse.getId()).eq("customer_id",goCustomer.getId()).eq("materiel_id",materielFlowMateriel.getId()));
                Integer materielRetained=0;
                if(null==goWarehouse){
                    goWarehouse.setCustomerId(goCustomer.getId());
                    goWarehouse.setWarehouseId(goCustomerWarehouse.getId());
                    goWarehouse.setMaterielId(materielFlowMateriel.getId());
                    goWarehouse.setMaterielNumber(materielList.get(i).getMaterielAmount());
                    warehouseService.insert(goWarehouse);
                }else{
                    materielRetained=goWarehouse.getMaterielNumber();
                    goWarehouse.setMaterielNumber(goWarehouse.getMaterielNumber()+materielList.get(i).getMaterielAmount());
                    warehouseService.updateById(goWarehouse);
                }

                //添加物料
                ErpClientsMaterielDetails materielDetails=new ErpClientsMaterielDetails();
                materielDetails.setPid(materielList.get(i).getId());//采购订单物料表
                materielDetails.setMaterielId(warehouse.getId());//仓库物料id
                materielDetails.setMaterielStudy(0);
                detailsService.insert(materielDetails);

                //添加物料流转统计信息
                ErpMaterielFlowSummary materielFlowSummary=new ErpMaterielFlowSummary();
                materielFlowSummary.setWarehouseId(goCustomerWarehouse.getId());
                materielFlowSummary.setMaterielId(materielFlowMateriel.getId());
                materielFlowSummary.setWorkSheetId(flow.getMaterielFlowSheetId());
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                materielFlowSummary.setMaterielFlowDate(df.format(new Date()));
                materielFlowSummary.setMaterielStorageNumber(materielList.get(i).getMaterielAmount());
                materielFlowSummary.setMaterielStoragePrice(materielList.get(i).getMaterielPrice());
                materielFlowSummary.setMaterielOutflowNumber(materielList.get(i).getMaterielAmount());
                materielFlowSummary.setMaterielOutflowPrice(materielList.get(i).getMaterielPrice());
                //materielFlowSummary.setIncludingTax();
                materielFlowSummary.setMaterielFlowType(1);
                materielFlowSummary.setMaterielRetained(materielRetained);
                materielFlowSummary.setOutWarehouseRetauned(num);  //数据库里没看见
                materielFlowSummary.setMaterielSource(customer.getCusName());
                materielFlowSummary.setMaterielWhereabouts(goCustomer.getCusName());
                materielFlowSummary.setOutWarehouseId(customerWarehouse.getId());
                flowSummaryService.insert(materielFlowSummary);

            }
            //物料出库采购订单对应表添加
            ErpFlowClients flowClients=new ErpFlowClients();
            flowClients.setClientsSheetid(clients.getSheetId());
            flowClients.setFlowSheetid(flow.getMaterielFlowSheetId());
            flowClients.setWorkState(1);
            flowClients.setDate(DataUtil.getStringDate());
            flowClientsService.insert(flowClients);

            //工作单添加
            ErpManagement management=new ErpManagement();
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            management.setDt(dateString);
            management.setWorkId(clients.getSheetId());
            management.setClientName(clients.getCustomerName());
            management.setWorkProject("被动出库");
            management.setSail(clients.getTotalMoney());
            management.setCustomerId(customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("cus_name",clients.getCustomerName())).getId());
            management.setAccounts(clients.getTotalMoney());
            //此处应该生成收款单
            management.setWorkPlace(customer.getCusName());
            managementService.insert(management);

            result.setCount(1);
            result.setData(1);
        }catch (Exception e){
            result.setCount(1);
            result.setData(0);
            e.printStackTrace();
        }
        return result;
    }



    @RequestMapping("/inertOrUpdateWarehouseList")
    @ApiOperation("添加Or修改仓库物料信息多条")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<List<ErpWarehouse>> inertOrUpdateWarehouseList(List<ErpWarehouse> customerCar) {
        Result<List<ErpWarehouse>> result=new Result<List<ErpWarehouse>>();
        try{
            warehouseService.insertOrUpdateAllColumnBatch(customerCar);
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }




    @RequestMapping("/inertOrUpdateWarehouse")
    @ApiOperation("添加Or修改仓库物料信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpWarehouse> inertOrUpdateWarehouse(ErpWarehouse customerCar) {
        Result<ErpWarehouse> result=new Result<ErpWarehouse>();
        try{
            warehouseService.insertOrUpdate(customerCar);
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getWarehouseByWarehouseId")
    @ApiOperation("根据仓库id仓库物料信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "warehouseId", value = "仓库ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpWarehouse>> getWarehouseByWarehouseId(Integer warehouseId) {
        Result<List<ErpWarehouse>> result=new Result<List<ErpWarehouse>>();
        try{
            List<ErpWarehouse> customerCar=warehouseService.selectList(new EntityWrapper<ErpWarehouse>().eq("warehouse_id",warehouseId));
            for (int i = 0; i <customerCar.size() ; i++) {
                customerCar.get(i).setFlowMateriel(flowMaterielService.selectById(customerCar.get(i).getMaterielId()));
            }
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }



    @RequestMapping("/getWarehouseBycustId")
    @ApiOperation("根据客户ID查询客户仓库信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpWarehouse>> getWarehouseBycustId(Integer custId) {
        Result<List<ErpWarehouse>> result=new Result<List<ErpWarehouse>>();
        try{
            List<ErpWarehouse> customerCars=warehouseService.selectList(new EntityWrapper<ErpWarehouse>().eq("customer_id",custId));
            
            for (int i = 0; i <customerCars.size() ; i++) {
                ErpMaterielFlowMateriel erpMaterielFlowMateriel = flowMaterielService.selectById(customerCars.get(i).getMaterielId());
                if(null!=erpMaterielFlowMateriel){
                    customerCars.get(i).setWuLiaoName(erpMaterielFlowMateriel.getMaterielName());
                    customerCars.get(i).setFlowMateriel(erpMaterielFlowMateriel);
                }

            }
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(customerCars.size());
            result.setData(customerCars);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getWarehouseBycustIdPage")
    @ApiOperation("根据客户ID查询仓库物料信息分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpWarehouse>> getWarehouseBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                            @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<Page<ErpWarehouse>> result=new Result<Page<ErpWarehouse>>();
        try{
            Page<ErpWarehouse> page1=new Page<ErpWarehouse>(page,limit);
            Page<ErpWarehouse> carPage=warehouseService.selectPage(page1,new EntityWrapper<ErpWarehouse>().eq("customer_id",custId));
            for (int i = 0; i <carPage.getRecords().size(); i++) {
                carPage.getRecords().get(i).setFlowMateriel(flowMaterielService.selectById(carPage.getRecords().get(i).getMaterielId()));
            }
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getWarehouseByWarehouseIdPage")
    @ApiOperation("根据仓库id仓库物料信息分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "warehouseId", value = "仓库ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpWarehouse>> getWarehouseByWarehouseIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                             @RequestParam(value="limit",defaultValue="10") int limit,Integer warehouseId) {
        Result<Page<ErpWarehouse>> result=new Result<Page<ErpWarehouse>>();
        try{
            Page<ErpWarehouse> page1=new Page<ErpWarehouse>(page,limit);
            Page<ErpWarehouse> carPage=warehouseService.selectPage(page1,new EntityWrapper<ErpWarehouse>().eq("warehouse_id",warehouseId));
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            for (int i = 0; i <carPage.getRecords().size(); i++) {
                carPage.getRecords().get(i).setFlowMateriel(flowMaterielService.selectById(carPage.getRecords().get(i).getMaterielId()));
                
            }
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getWarehouseMaterielBycustId")
    @ApiOperation("根据客户id查询相关仓库物料库存信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),*/
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpWarehouse>> getWarehouseMaterielBycustId(String startDate,String endDate,Integer custId) {
        Result<List<ErpWarehouse>> result=new Result<List<ErpWarehouse>>();
        try{
            /*ErpCustomer customer=customerService.selectById(custId);
            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",customer.getCusCode()).or().eq("cus_service_com",customer.getCusCode()));
            customers.add(customer);
            List<Integer> intIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());*/
            String strids=customerService.getcustOfMe(custId);
            List<ErpCustomerWarehouse> customerWarehouses=customerWarehouseService.selectList(new EntityWrapper<ErpCustomerWarehouse>().in("customer_id",strids).between("war_date",startDate,endDate));
            List<Integer> wareIds=customerWarehouses.stream().map(ErpCustomerWarehouse::getId).collect(Collectors.toList());
            String strWareIds= BlankUtil.listToString(wareIds);
            List<ErpWarehouse> warehouseList=warehouseService.selectList(new EntityWrapper<ErpWarehouse>().in("warehouse_id",strWareIds));//获取相关仓库中的物料种类信息
            for (int i = 0; i < warehouseList.size(); i++) {//获取入库数量
                Integer outMatNum=flowSummaryService.getMatOutNum(warehouseList.get(i).getMaterielId(),warehouseList.get(i).getWarehouseId(),startDate,endDate);//出库总数
                Integer inMatNum=flowSummaryService.getMatInNum(warehouseList.get(i).getMaterielId(),warehouseList.get(i).getWarehouseId(),startDate,endDate);//入库总数
                List<ErpMaterielFlowSummary> warehouseList1=flowSummaryService.getSummary(warehouseList.get(i).getMaterielId(),warehouseList.get(i).getWarehouseId(),startDate);
                if(0<warehouseList1.size()){
                    Integer matNum=0;
                    if(warehouseList.get(i).getWarehouseId()==warehouseList1.get(0).getWarehouseId()){
                        matNum=warehouseList1.get(0).getMaterielStorageNumber();
                    }else{
                        matNum=warehouseList1.get(0).getMaterielOutflowNumber();
                    }
                    warehouseList.get(i).setMatNum(matNum);
                }
                warehouseList.get(i).setOutMatNum(outMatNum);
                warehouseList.get(i).setInMatNum(inMatNum);
                warehouseList.get(i).setFlowMateriel(flowMaterielService.selectById(warehouseList.get(i).getMaterielId()));
            }
            List<ErpWarehouse> warehouseListEnd=new ArrayList<>();
            for (int i = 0; i < warehouseList.size(); i++) {
                ErpWarehouse erpWarehouse =  warehouseList.get(i);
                for (int j = 0; j < warehouseList.size(); j++) {
                    if (i!=j){
                        ErpWarehouse warehouse =  warehouseList.get(j);
  if (erpWarehouse.getMaterielBrand().equals(warehouse.getMaterielBrand())&&
          erpWarehouse.getMaterielSpecifications().equals(warehouse.getMaterielSpecifications())
          &&erpWarehouse.getMaterielModel().equals(warehouse.getMaterielModel())
          &&erpWarehouse.getMaterielPlace().equals(warehouse.getMaterielPlace())&&
          erpWarehouse.getMaterielState().equals(warehouse.getMaterielState())
          &&erpWarehouse.getMaterielUnit().equals(warehouse.getMaterielUnit())
          ){
      erpWarehouse.setMaterielNumber(erpWarehouse.getMaterielNumber()+warehouse.getMaterielNumber());
      erpWarehouse.setMaterielTax(erpWarehouse.getMaterielTax()+warehouse.getMaterielTax()/2);
      erpWarehouse.setMaterielNotax(erpWarehouse.getMaterielNotax()+warehouse.getMaterielNotax()/2);
      erpWarehouse.setInMatNum(erpWarehouse.getInMatNum()+warehouse.getInMatNum());
      erpWarehouse.setOutMatNum(erpWarehouse.getOutMatNum()+warehouse.getOutMatNum());
                  warehouseList.remove(j);


                        }

                    }


                }
            }


            result.setCount(warehouseList.size());

            result.setData(warehouseList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getWarehouseMaterielToTwo")
    @ApiOperation("查询相关仓库物料库存物料查看")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),*/
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "matId", value = "物料id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpWarehouse>> getWarehouseMaterielToTwo(String startDate,String endDate,Integer custId,Integer matId) {
        Result<List<ErpWarehouse>> result=new Result<List<ErpWarehouse>>();
        try{
            ErpCustomer customer=customerService.selectById(custId);
            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_sup_company",customer.getCusCode()).or().eq("cus_service_com",customer.getCusCode()));
            customers.add(customer);
            List<Integer> intIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
            String strids=BlankUtil.listToString(intIds);
            List<ErpCustomerWarehouse> customerWarehouses=customerWarehouseService.selectList(new EntityWrapper<ErpCustomerWarehouse>().in("customer_id",strids));
            List<Integer> wareIds=customerWarehouses.stream().map(ErpCustomerWarehouse::getId).collect(Collectors.toList());
            String strWareIds= BlankUtil.listToString(wareIds);
            List<ErpWarehouse> warehouseList=warehouseService.selectList(new EntityWrapper<ErpWarehouse>().eq("materiel_id",matId).in("warehouse_id",strWareIds));//获取相关仓库中的物料种类信息
            for (int i = 0; i < warehouseList.size(); i++) {//获取入库数量
                Integer outMatNum=flowSummaryService.getMatOutNum(warehouseList.get(i).getMaterielId(),warehouseList.get(i).getWarehouseId(),startDate,endDate);//出库总数
                Integer inMatNum=flowSummaryService.getMatInNum(warehouseList.get(i).getMaterielId(),warehouseList.get(i).getWarehouseId(),startDate,endDate);//入库总数
                List<ErpMaterielFlowSummary> warehouseList1=flowSummaryService.getSummary(warehouseList.get(i).getMaterielId(),warehouseList.get(i).getWarehouseId(),startDate);
                if(0<warehouseList1.size()){
                    Integer matNum=0;
                    if(warehouseList.get(i).getWarehouseId()==warehouseList1.get(0).getWarehouseId()){
                        matNum=warehouseList1.get(0).getMaterielStorageNumber();
                    }else{
                        matNum=warehouseList1.get(0).getMaterielOutflowNumber();
                    }
                    warehouseList.get(i).setMatNum(matNum);
                }
                warehouseList.get(i).setOutMatNum(outMatNum);//本期出库
                warehouseList.get(i).setCustName(customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("id",warehouseList.get(i).getCustomerId())).getCusName());
                warehouseList.get(i).setWareName(customerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("id",warehouseList.get(i).getWarehouseId())).getWarName());
                warehouseList.get(i).setInMatNum(inMatNum);//本期入库
                warehouseList.get(i).setFlowMateriel(flowMaterielService.selectById(warehouseList.get(i).getMaterielId()));
            }

            result.setCount(warehouseList.size());
            result.setData(warehouseList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getWarehouseMaterielToDate")
    @ApiOperation("查询相关仓库相关物料在一段时间内得库存情况")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),*/
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "matId", value = "物料id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "wareId", value = "仓库ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpMaterielFlowSummary>> getWarehouseMaterielToDate(String startDate,String endDate,Integer wareId,Integer matId) {
        Result<List<ErpMaterielFlowSummary>> result=new Result<List<ErpMaterielFlowSummary>>();
        try{
            List<ErpMaterielFlowSummary> warehouseList=flowSummaryService.getSummaryList(matId,wareId,startDate,endDate);//
            result.setCount(warehouseList.size());
            result.setData(warehouseList);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getOutMaterielToDate")
    @ApiOperation("查询相关仓库相关物料在一段时间内得出库情况")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),*/
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "matId", value = "物料id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "wareId", value = "仓库ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<Map<String, Object>>> getOutMaterielToDate(String startDate,String endDate,Integer wareId,Integer matId) {
        Result<List<Map<String, Object>>> result=new Result<List<Map<String, Object>>>();
        List<Map<String, Object>> list =new ArrayList<>();
        try{
            List<ErpMaterielFlowSummary> warehouseList=flowSummaryService.getOutMaterielToDate(matId,wareId,startDate,endDate);//
            for (int i = 0; i < warehouseList.size(); i++) {//materielUnit单位
                ErpMaterielFlowSummary erpMaterielFlowSummary =  warehouseList.get(i);
                //ErpMateriel erpMateriel = erpMaterielService.selectById(erpMaterielFlowSummary.getMaterielId());
                /*if(erpMateriel==null||erpMateriel.getMaterielUnit()==null) {
                	erpMaterielFlowSummary.setMaterielUnit("");
                }else {
                	erpMaterielFlowSummary.setMaterielUnit(erpMateriel.getMaterielUnit());
                }*/
                Map<String, Object> map=new HashMap<>();
                map.put("time", erpMaterielFlowSummary.getMaterielFlowDate());
                map.put("sheeId", erpMaterielFlowSummary.getWorkSheetId());
                map.put("subject", erpMaterielFlowSummary.getFlowSubject());
                map.put("num", erpMaterielFlowSummary.getMaterielStorageNumber());
                map.put("price", erpMaterielFlowSummary.getMaterielStoragePrice());
                map.put("totalPrice", erpMaterielFlowSummary.getMaterielStorageNumber()*erpMaterielFlowSummary.getMaterielStoragePrice());
                if(erpMaterielFlowSummary.getFlowSubject().equals("销售维修")){
                	if(erpMaterielFlowSummary.getWorkSheetId()!=null) {
                		List<ErpSaleRepairOrder> orderList= erpSaleRepairOrderMapper.selectList(new EntityWrapper<ErpSaleRepairOrder>().eq("work_sheet_id", erpMaterielFlowSummary.getWorkSheetId()).orderBy("id", false));
                		if(orderList!=null&&orderList.size()>0) {
                			ErpSaleRepairOrder order=orderList.get(0);
                			int custId=order.getCustomerId();
                			ErpCustomer cust=customerService.selectById(custId);
                			map.put("workUnit", cust.getCusName());
                            map.put("custName", order.getCustomerName());
                            map.put("carPlate", order.getCarPlate());
                            List<ErpSaleRepairRepair> reList=erpSaleRepairRepairMapper.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", erpMaterielFlowSummary.getWorkSheetId()));
                            String subject="";
                            for (ErpSaleRepairRepair c : reList) {
                            	subject=subject+c.getRepairRepair()+",";
							}
                            subject=subject.substring(0, subject.length()-1);
                            map.put("subject", subject);
                		}else {
                			map.put("workUnit", "");
                            map.put("custName", "");
                            map.put("carPlate", "");
                		}
                }else {
                	ErpMaterielFlow flow=flowService.selectOne(new EntityWrapper<ErpMaterielFlow>().eq("materiel_flow_sheet_id", erpMaterielFlowSummary.getWorkSheetId()));
                	map.put("workUnit", flow.getTransportCustomer());
                    map.put("custName", flow.getSourceCustomerName());
                    map.put("carPlate", flow.getTransportCar());
                }
                }
               list.add(map);
                //拼接map
            }
            result.setCount(list.size());
            result.setData(list);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getGoMaterielToDate")
    @ApiOperation("查询相关仓库相关物料在一段时间内得入库情况")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),*/
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "matId", value = "物料id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "wareId", value = "仓库ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<Map<String, Object>>> getGoMaterielToDate( @RequestParam(value="startDate",defaultValue="1000-01-01") String startDate,@RequestParam(value="endDate",defaultValue="2099-01-01") String endDate,Integer wareId,Integer marId) {
        Result<List<Map<String, Object>>> result=new Result<List<Map<String, Object>>>();
        try{
        	List<Map<String, Object>> list =new ArrayList<>();
            List<ErpMaterielFlowSummary> warehouseList=flowSummaryService.getGoMaterielToDate(marId,wareId,startDate,endDate);//
            for (int i = 0; i < warehouseList.size(); i++) {//materielUnit单位
                ErpMaterielFlowSummary erpMaterielFlowSummary =  warehouseList.get(i);
                //ErpMateriel erpMateriel = erpMaterielService.selectById(erpMaterielFlowSummary.getMaterielId());
                /*if(erpMateriel==null||erpMateriel.getMaterielUnit()==null) {
                	erpMaterielFlowSummary.setMaterielUnit("");
                }else {
                	erpMaterielFlowSummary.setMaterielUnit(erpMateriel.getMaterielUnit());
                }*/
                Map<String, Object> map=new HashMap<>();
                map.put("time", erpMaterielFlowSummary.getMaterielFlowDate());
                map.put("sheeId", erpMaterielFlowSummary.getWorkSheetId());
                map.put("subject", erpMaterielFlowSummary.getFlowSubject());
                map.put("num", erpMaterielFlowSummary.getMaterielStorageNumber());
                map.put("price", erpMaterielFlowSummary.getMaterielStoragePrice());
                map.put("totalPrice", erpMaterielFlowSummary.getMaterielStorageNumber()*erpMaterielFlowSummary.getMaterielStoragePrice());
                if(erpMaterielFlowSummary.getFlowSubject().equals("期初物料")) {
                	 map.put("workUnit", "");
                     map.put("custName", "");
                     map.put("carPlate", "");
                }else if(erpMaterielFlowSummary.getFlowSubject().equals("销售维修")){
                	if(erpMaterielFlowSummary.getWorkSheetId()!=null) {
                		List<ErpSaleRepairOrder> orderList= erpSaleRepairOrderMapper.selectList(new EntityWrapper<ErpSaleRepairOrder>().eq("work_sheet_id", erpMaterielFlowSummary.getWorkSheetId()).orderBy("id", false));
                		if(orderList!=null&&orderList.size()>0) {
                			ErpSaleRepairOrder order=orderList.get(0);
                			int custId=order.getCustomerId();
                			ErpCustomer cust=customerService.selectById(custId);
                			map.put("workUnit", cust.getCusName());
                            map.put("custName", order.getCustomerName());
                            map.put("carPlate", order.getCarPlate());
                            List<ErpSaleRepairRepair> reList=erpSaleRepairRepairMapper.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", erpMaterielFlowSummary.getWorkSheetId()));
                            String subject="";
                            for (ErpSaleRepairRepair c : reList) {
                            	subject=subject+c.getRepairRepair()+",";
							}
                            subject=subject.substring(0, subject.length()-1);
                            map.put("subject", subject);
                		}else {
                			map.put("workUnit", "");
                            map.put("custName", "");
                            map.put("carPlate", "");
                		}
                	}else {
                		map.put("workUnit", "");
                        map.put("custName", "");
                        map.put("carPlate", "");
                	}
                }
               list.add(map);
                //拼接map
            }
            result.setCount(list.size());
            result.setData(list);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getMaterielsCk")
    @ApiOperation("期初物料查询列表")
    @ApiImplicitParam(name = "ckid", value = "客户ID", required = false, dataType = "int", paramType = "query")
    public Result<List<Map<String, Object>>> getMaterielsCk(int ckid){
        return customerWarehouseService.getMaterielsCk(ckid);
    }

    @RequestMapping("/getMaterielsCkqb")
    @ApiOperation("期初物料查询列表")
    @ApiImplicitParam(name = "ckid", value = "客户ID", required = false, dataType = "int", paramType = "query")
    public Result<List<Map<String, Object>>> getMaterielsCkqb(int ckid){
        return customerWarehouseService.getMaterielsCkqb(ckid);
    }

    //查询库存物料
    @RequestMapping("/getMaterielszi")
    @ApiOperation("期初物料查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "khid", value = "客户id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ckid", value = "库房id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "materiel_name", value = "品名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "materiel_brand", value = "品牌", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "materiel_Specifications", value = "规格", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "materiel_model", value = "型号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "materiel_place", value = "产地", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "materiel_unit", value = "单位", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "typekcorlc", value = "类型", required = false, dataType = "String", paramType = "query")

    })
    public Result<List<Map<String, Object>>> getMaterielszi(String khid,String ckid,String materiel_name,String materiel_brand,String materiel_Specifications,String materiel_model,String materiel_place,String materiel_unit,String typekcorlc){
        return customerWarehouseService.getMaterielszi(khid,ckid,materiel_name,materiel_brand,materiel_Specifications,materiel_model,materiel_place,materiel_unit,typekcorlc);
    }


    @RequestMapping("/getMateriels")
    @ApiOperation("期初物料查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "typestr", value = "查询物料维修", required = false, dataType = "String", paramType = "query")
    })
    public Result<List<Map<String, Object>>> getMateriels(int custId,String typestr){
    	return customerWarehouseService.getMateriels(custId,typestr);
    }
    
    @RequestMapping("/getErrorMateriels")
    @ApiOperation("数据纠错期初物料查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "proId", value = "生产编号", required = false, dataType = "String", paramType = "query")
    })
    public Result<List<Map<String, Object>>> getErrorMateriels(int custId,@RequestParam(value="proId",defaultValue="")String proId){
    	return customerWarehouseService.getErrorMateriels(custId, proId);
    }

    @RequestMapping("/getMateriels_xswx")
    @ApiOperation("期初物料查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ckid", value = "仓库ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "typestr", value = "查询物料s维修", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "kehuid", value = "客户id", required = false, dataType = "int", paramType = "query"),
    })
    public Result<List<Map<String, Object>>> getMateriels_xswx(int ckid,String typestr,int kehuid){
        return customerWarehouseService.getMateriels_xswx(ckid,typestr,kehuid);
    }
    //查询留存物料 根据客户id 仓库id
    @RequestMapping("/getMateriels_xswx_lc")
    @ApiOperation("期初物料查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ckid", value = "仓库ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "khid", value = "客户id", required = false, dataType = "int", paramType = "query")
    })
    public Result<List<Map<String, Object>>> getMateriels_xswx_lc(int ckid,int khid){
        return customerWarehouseService.getMateriels_xswx_lc(ckid,khid);
    }
    
    @RequestMapping("/saveMateriels")
    @ApiOperation("期初物料添加")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "materiel", value = "实体类", required = false, dataType = "ErpMateriel", paramType = "query"),
         @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query"),
         @ApiImplicitParam(name = "materielNumber", value = "数量", required = false, dataType = "int", paramType = "query"),
         @ApiImplicitParam(name = "faPiao", value = "发票类型", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "cangkuName", value = "仓库名", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "jingban", value = "经办人", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "shengIds", value = "生产编号json", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "custName", value = "客户名", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "qrcodes", value = "二维码", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "rfIds", value = "rfid", required = false, dataType = "String", paramType = "query"),

         @ApiImplicitParam(name = "taiJson", value = "传感器实体类json", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "chuanIds", value = "传感器生产编号json", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "qrChuanCodes", value = "传感器二维码", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "rfChuanIds", value = "传感器rfid", required = false, dataType = "String", paramType = "query")
    })
    public Result<Integer> saveMateriels(@ModelAttribute ErpMateriel materiel,Double allPrice,int custId,int materielNumber,String faPiao,String cangkuName,String jingban,String shengIds,String custName,String qrcodes,String rfIds,String taiJson,String chuanIds,String qrChuanCodes,String rfChuanIds){
    	String[] sList=shengIds.split(",");
    	String[] qrList=qrcodes.split(",");
    	String[] rfList=rfIds.split(",");
//        String[] taiyaList=tpms.split(","); //胎压id
    	return warehouseService.saveMateriels(materiel,allPrice, custId,materielNumber, faPiao, cangkuName, jingban, sList ,qrList,rfList, custName,taiJson,chuanIds,qrChuanCodes,rfChuanIds);
    }
    
    @RequestMapping("/selectMaterielById")
    @ApiOperation("根据id查物料信息")
    @ApiImplicitParam(name = "materielId", value = "物料id", required = false, dataType = "int", paramType = "query")
    public Result<Map<String, Object>> selectMaterielById(int materielId){
    	Map<String, Object> map=new HashMap<>();
    	ErpMateriel materiel=erpMaterielService.selectById(materielId);
    	map.put("materiel", materiel);
    	return new Result<Map<String,Object>>(map, 1);
    }
    
    @RequestMapping("/selectMaterielByPlate")
    @ApiOperation("根据车id查所有物料信息")
    @ApiImplicitParam(name = "carId", value = "车辆id", required = false, dataType = "int", paramType = "query")
    public Result<List<ErpMateriel>> selectByCarId(int carId){
    	return erpMaterielService.selectByCarId(carId);
    }
    
    @RequestMapping("/selectByCarPlate")
    @ApiOperation("根据车牌号加胎位查所有物料信息")
    @ApiImplicitParam(name = "carIdAndPlate", value = "车牌号加胎位(津KYD178-左1轮)", required = false, dataType = "int", paramType = "query")
    public Result<ErpMateriel> selectByCarPlate(String carIdAndPlate){
        //          String [] a=  carIdAndPlate.split("-");
//            ErpCustomerCar erpCustomerCar = erpCustomerCarService.selectOne(new EntityWrapper<ErpCustomerCar>().eq("car_plate", a[0]));
//            ErpCarTireArchives erpCarTireArchives = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", erpCustomerCar.getId()).eq("tire_location", a[1]));
//            ErpCustomerWarehouse customer_id = erpCustomerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id", erpCustomerCar.getCustomerId()));
//            ErpTireSensor erpTireSensor = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("tire_id", erpCarTireArchives.getTireId()).eq("warehouse_id", customer_id.getId()));
//           if (erpTireSensor!=null){
//               if (erpTireSensor.getSensorId()!=0){
//                   ErpMateriel erpMateriel = erpMaterielService.selectById(erpTireSensor.getSensorId());
//                   l.get(0).setChuanganqiNumber(erpMateriel.getMaterielProductionId());
//               }
//           }

        String[] groupId=carIdAndPlate.split("-");//把上面的去向截取
        ErpCustomerCar cusCar =erpCustomerCarMapper.selectByPlate(groupId[0]);
        ErpCarTireArchives car_id = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCar.getId()).eq("tire_location",groupId[1]));//.eq("car_id", cusCar.getId()

        List<ErpMateriel> erpMaterielList=new ArrayList<ErpMateriel>();
//                erp_tire_archives_work
//                ErpTireArchivesWork tire_location = erpTireArchivesWorkService.selectOne(new EntityWrapper<ErpTireArchivesWork>().eq("tire_location", erpSaleRepairSale.getMaterielOutTo()));
        ErpMateriel erpMateriel1 = erpMaterielService.selectById(car_id.getTireId());
//        List<ErpSaleRepairRepair> l=new ArrayList<>();
//    	List<ErpSaleRepairRepair> list=erpSaleRepairRepairMapper.selectList(new EntityWrapper<ErpSaleRepairRepair>().like("materiel_out_to", carIdAndPlate).orderBy("id", false));
//        list.parallelStream().forEach( ls->{
//            if (ls.getMaterielName().contains("轮胎")){
//                l.add(ls);
//            }
//        });
//
//    	if(l.size()>0) {
//    		return new Result<ErpSaleRepairRepair>(l.get(0), 1);
//    	}else {
//    		return new Result<ErpSaleRepairRepair>(null, 0);
//    	}
        if (erpMateriel1!=null){
            return  new Result<>(erpMateriel1,1);
        }else {
            return  new Result<>(null,0);
        }
    }

    @RequestMapping("/selectByCarPlatelxl")
    @ApiOperation("根据车牌号加胎位查所有物料信息")
    @ApiImplicitParam(name = "carIdAndPlate", value = "车牌号加胎位(津KYD178-左1轮)", required = false, dataType = "int", paramType = "query")
    public Result<List<ErpMateriel>> selectByCarPlatelxl(String carIdAndPlate){
        Result<List<ErpMateriel>> result=new Result<List<ErpMateriel>>();
        List<ErpMateriel> erpMaterielList=new ArrayList<ErpMateriel>();
        String[] groupId=carIdAndPlate.split("-");//把上面的去向截取
        ErpCustomerCar cusCar =erpCustomerCarMapper.selectByPlate(groupId[0]);
        ErpCarTireArchives car_id = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCar.getId()).eq("tire_location",groupId[1]));//.eq("car_id", cusCar.getId()
        ErpTireSensor  erpTireSensor =erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("tire_id",car_id.getTireId()));
        if(erpTireSensor!=null) {
            ErpMateriel erpMateriel = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpTireSensor.getTireId()));
            erpMaterielList.add(erpMateriel);
            if (erpTireSensor.getRimsId() != null && !erpTireSensor.getRimsId().equals("")) {
                erpMateriel = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpTireSensor.getRimsId()));
                erpMaterielList.add(erpMateriel);
            }
            if (erpTireSensor.getSensorId() != null && !erpTireSensor.getSensorId().equals("")) {
                erpMateriel = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpTireSensor.getSensorId()));
                erpMaterielList.add(erpMateriel);
            }
            if (erpTireSensor.getSensorId1() != null && !erpTireSensor.getSensorId1().equals("")) {
                erpMateriel = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpTireSensor.getSensorId1()));
                erpMaterielList.add(erpMateriel);
            }

        }



        result.setCount(erpMaterielList.size());
        result.setData(erpMaterielList);
        return  result;

    }
    
    @RequestMapping("/selectByBranch")
    @ApiOperation("经营管理-物料库存")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "warehouseIds", value = "仓库id(,号间隔)", required = false, dataType = "ErpMateriel", paramType = "query"),
        @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "name", value = "物料名 没有不填", required = false, dataType = "String", paramType = "query")
   })
    public Result<List<String[]>> selectByBranch(String warehouseIds, @RequestParam(name="startTime",defaultValue="")String startTime, @RequestParam(name="endTime",defaultValue="")String endTime, @RequestParam(name="name",defaultValue="")String name){
    	String[] ids=warehouseIds.split(",");
    	List<Integer> list=new ArrayList<>();
    	for(String id:ids) {
    		list.add(Integer.parseInt(id));
    	}
    	return erpMaterielService.selectByBranch(list, startTime, endTime, name);
    }
    
    @RequestMapping("/findByLibraryName")
    @ApiOperation("经营管理-物料库存-查看详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "warehouseIds", value = "仓库id(,号间隔)", required = false, dataType = "ErpMateriel", paramType = "query"),
        @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "wareId", value = "上一接口返回的数组[0]", required = false, dataType = "String", paramType = "query")
   })
    public Result<Map<String, Object>> findByLibraryName(int wareId,String warehouseIds,String startTime,String endTime){
    	String[] ids=warehouseIds.split(",");
    	List<Integer> list=new ArrayList<>();
    	for(String id:ids) {
    		list.add(Integer.parseInt(id));
    	}
    	return erpMaterielService.findByLibraryName(wareId, list, startTime, endTime);
    }
    
    @RequestMapping("/selectByBranchWarn")
    @ApiOperation("经营管理-物料库存带预警值的")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "warehouseIds", value = "仓库id", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query")
   })
    public Result<List<String[]>> selectByBranchWarn(Integer warehouseIds,String startTime,String endTime){
    	return erpMaterielService.selectByBranchWarn(warehouseIds, startTime, endTime);
    }
    
    @RequestMapping("/shezhiWarn")
    @ApiOperation("经营管理-物料库存设置预警值")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "wareId", value = "仓库id", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "materielId", value = "数组[0]", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "materielWarnAmount", value = "预警值", required = false, dataType = "int", paramType = "query")
   })
    public Result<Integer> shezhiWarn(int materielId,int custId,int wareId,int materielWarnAmount){
    	return erpMaterielService.shezhiWarn(materielId, custId, wareId, materielWarnAmount);
    }



    //采购订单 用户出库提交操作
    @RequestMapping("/caigoudingdanchuku_insert")
    @ApiOperation("采购订单 用户出库提交")
    @ApiImplicitParams({
           @ApiImplicitParam(name = "wlid", value = "物料id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "caigoudingdanid", value = "采购订单id", required = false, dataType = "int", paramType = "query"),
    })
    public @ResponseBody
    String caigoudingdanchuku_insert(String scbh,int caigoudingdanid) {
//        Result<ErpWarehouse> result=new Result<ErpWarehouse>();
//        try{
//            warehouseService.insertOrUpdate(customerCar);
//            result.setCount(1);
//            result.setData(customerCar);
//        }catch (Exception e){e.printStackTrace();}
        return "";
    }

    @RequestMapping("/selectDateils")
    @ApiOperation("经营管理-物料库存-物料库存查看明细")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "wareId", value = "仓库物料id", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "warehouseIds", value = "仓库id", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query")
   })
    public Result<List<Map<String, Object>>> selectDateils(String warehouseIds,int wareId,String startTime,String endTime){
    	return erpMaterielService.selectDateils(warehouseIds, wareId, startTime, endTime);
    }
    
    @RequestMapping("/editMateriel")
    public Result<Boolean> editMateriel(@ModelAttribute ErpMateriel materiel){
    	Boolean pan=false;
    	List<ErpMaterielCopy> copyList=erpMaterielCopyMapper.selectList(new EntityWrapper<ErpMaterielCopy>().eq("materiel_id", materiel.getId()).orderBy("id", false));
    	if(copyList!=null&&copyList.size()>0) {
    		ErpMaterielCopy copy=copyList.get(0);
    		copy.setMaterielStorageDate(materiel.getMaterielStorageDate());
    		copy.setMaterielName(materiel.getMaterielName());
    		copy.setMaterielBrand(materiel.getMaterielBrand());
    		copy.setMaterielSpecifications(materiel.getMaterielSpecifications());
    		copy.setMaterielModel(materiel.getMaterielModel());
    		copy.setMaterielPlace(materiel.getMaterielPlace());
    		copy.setMaterielState(materiel.getMaterielState());
    		copy.setMaterielUnit(materiel.getMaterielUnit());
    		copy.setMaterielProductionId(materiel.getMaterielProductionId());
    		copy.setMaterielEqcode(materiel.getMaterielEqcode());
    		copy.setMaterielRfid(materiel.getMaterielRfid());
    		erpMaterielCopyMapper.updateById(copy);
    	}
    	pan=erpMaterielService.updateById(materiel);
    	return new Result<Boolean>(pan, 1);
    }
}

